//
//  LGFollowPerationView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/9/22.
//

#import "LGFollowOperationView.h"

@interface LGFollowOperationView()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIButton *closeButton;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIButton *sureButton;

@end

@implementation LGFollowOperationView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.hidden = YES;
        [self addSubview:self.baseView];
        [self.baseView addSubview:self.titleLabel];
        [self.baseView addSubview:self.closeButton];
        [self.baseView addSubview:self.tableView];
        [self.baseView addSubview:self.sureButton];
        [self showViewAnimation];
    }
    return self;
}

-(void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    [self.tableView reloadData];
}

-(void)closeButtonAction{
    [self hiddenViewAnimation];
}

//确定
-(void)sureButtonAction{
    if(self.selectOperationChanged){
        self.selectOperationChanged(self.codeArray);
    }
    [self hiddenViewAnimation];
}

//出现弹框
-(void)showViewAnimation{
    self.hidden = NO;
    [UIView animateWithDuration:0.4 animations:^{
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0.5];
        self.baseView.frame = CGRectMake(0, viewPix(234), Screen_W, Screen_H-viewPix(220));
    }];
}

//隐藏弹框
-(void)hiddenViewAnimation{
    [UIView animateWithDuration:0.4 animations:^{
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
        self.baseView.frame = CGRectMake(0, Screen_H, Screen_W, Screen_H-viewPix(220));
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGFollowOperationViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = self.dataArray[indexPath.row];
    NSString *code = LGNSString(self.dataArray[indexPath.row][@"code"]);
    cell.isSelect = [self.codeArray containsObject:code]?YES:NO;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *code = LGNSString(self.dataArray[indexPath.row][@"code"]);
    if([self.codeArray containsObject:code]){
        [self.codeArray removeObject:code];
    }else{
        [self.codeArray addObject:code];
    }
    [self.tableView reloadData];
}

#pragma mark
#pragma mark ====> 创建控件
-(NSMutableArray *)codeArray{
    if(!_codeArray){
        _codeArray = [NSMutableArray array];
    }
    return _codeArray;
}
-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc]initWithFrame:CGRectMake(0, Screen_H, Screen_W, Screen_H-viewPix(220))];
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.cornerRidus = 15;
    }
    return _baseView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"手术类型" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
        _titleLabel.frame = CGRectMake(viewPix(50), viewPix(20), Screen_W-viewPix(100), viewPix(22));
    }
    return _titleLabel;
}

-(UIButton *)closeButton{
    if(!_closeButton){
        _closeButton = [UIButton buttonWithImage:@"closeGray16" selectImage:@"closeGray16"];
        _closeButton.frame = CGRectMake(Screen_W-viewPix(56), viewPix(15), viewPix(46), viewPix(46));
        [_closeButton addTarget:self action:@selector(closeButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeButton;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, viewPix(50), Screen_W, Screen_H-viewPix(234)-viewPix(50)-viewPix(64)-bottomSafeBarHeight/3.0) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGFollowOperationViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
    }
    return _tableView;
}

-(UIButton *)sureButton{
    if(!_sureButton){
        _sureButton = [UIButton buttonWithTitle:@"确定" topMargin:kMaxY(self.tableView.frame)];
        [_sureButton addTarget:self action:@selector(sureButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureButton;
}

@end


@interface LGFollowOperationViewCell()

/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIButton *selectButton;

@end

@implementation LGFollowOperationViewCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.selectButton];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(viewPix(20));
            make.top.equalTo(self.contentView).offset(viewPix(8));
            make.bottom.equalTo(self.contentView).offset(-viewPix(8));
            make.right.equalTo(self.contentView).offset(-viewPix(50));
        }];
        [self.selectButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).offset(-viewPix(20));
            make.centerY.equalTo(self.contentView);
            make.width.height.equalTo(@(viewPix(30)));
        }];
    }
    return  self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.titleLabel.text = LGNSString(dataDic[@"name"]);
}

-(void)setIsSelect:(BOOL)isSelect{
    _isSelect = isSelect;
    self.selectButton.selected = isSelect;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFontWeight(14, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _titleLabel;
}

-(UIButton *)selectButton{
    if(!_selectButton){
        _selectButton = [UIButton buttonWithImage:@"unselect" selectImage:@"selected"];
        _selectButton.userInteractionEnabled = NO;
    }
    return _selectButton;
}

@end
